//专门负责路由
var selectNum
var fileName
var registrant
var Id
var pos
angular.module('person',['ngRoute', 'NetworkRequest'])
.config(['$routeProvider','$locationProvider',  function ($routeProvider, $locationProvider) {
    $locationProvider.hashPrefix("");
    $routeProvider
        .when('/addEmployee',{
            // template:"登记",
            //相对于index.html
            // templateUrl: "./public/addEmployee.html"
            templateUrl: "/views/addEmployee"

        })
        .when('/selectEmployee', {
            // template:"查询",
            templateUrl: "/views/selectEmployee"

        })
        .when('/resultEmployee', {
            // template:"结果",
            templateUrl: "/views/resultEmployee"

        }).when('/updateEmployee', {
        // template:"结果",
        templateUrl: "/views/updateEmployee"

        })
}])

//人事资源管理登记
.controller('addEmployeeCtrl', ['$scope', '$http', function ($scope, $http){
    //获取创建时间
    $scope.creatTime = new Date().getTime()
    //获取登记人
    $scope.registrant = document.getElementById('sysName').innerHTML
    registrant = $scope.registrant
    //获取登记人的Id
    $scope.Id = document.getElementById('ID').innerHTML
    Id = $scope.Id
    //获取登记人的职位
    $scope.pos = document.getElementById('sysPos').innerHTML
    pos =  $scope.pos
    console.log($scope.registrant, $scope.Id )
/*-------------------------------------------------------设置初值-----------------------------------------*/
    $scope.employeeName = 1,
        $scope.age = 1,
        $scope.sex = "男",
        $scope.email = '1',
        $scope.phone = '1',
        $scope.qq = '1',
        $scope.telephone ='1',
        $scope.address = '1',
        $scope.postCode = '1',
        $scope.nationality = '中国',
        $scope.birthPlace = '广东省广州市',
        // $scope.birthday = '1997-09-20',
        $scope.nation = "汉族",
        $scope.faith = '1',
        $scope.politicalStatus = '1',
        $scope.idcard = '1',
        $scope.soSeNum = '1',
        $scope.edubg = '本科',
        $scope.major = '软件工程',
        $scope.bank = '1',
        $scope.accountNumber ='1',
        $scope.hobby = '学习',
        $scope.resume = '1',
        $scope.familyRelative ='1',
        $scope.remark = '1',
        $scope.first = '01',
        $scope.second ='01',
        $scope.third = '11',

        $scope.professional = 1
        $scope.position_Type = []
        $scope.position_Name = []
        $scope.position_Title = []

        /*-------------------------------------------------------设置初值-----------------------------------------*/

        // 获取所有的值
// console.log($scope.addEmployee)

    //获取所有机构名
    $scope.selectOrg(function (res) {
        console.log(res)
        $scope.firstOrgList = res.first_org
        $scope.sencondOrgList = res.second_org
        $scope.thirdOrgList = res.third_org
    },function (err) {
        console.log(err)
    })

    //获取所有职称名
    $scope.selectPosition(function (res) {
        console.log(res)
         $scope.position = res.data



        //循环数组
        for(var i = 0; i <= res.data.length-1; i++){

            // //获取职位分类
            for(var j = 0; j <=  $scope.position_Type.length; j++){
                        if( $scope.position_Type.indexOf(res.data[i].position_type) == -1){
                            $scope.position_Type.push(res.data[i].position_type)
                        }
                    }
            //获取职位名称
            for(var j = 0; j <=  $scope.position_Name.length; j++){
                if( $scope.position_Name.indexOf(res.data[i].position_name) == -1){
                    $scope.position_Name.push(res.data[i].position_name)
                }
            }
            //职位称呼
            for(var j = 0; j <=  $scope.position_Title.length; j++){
                if( $scope.position_Title.indexOf(res.data[i].position_title) == -1){
                    $scope.position_Title.push(res.data[i].position_title)
                }
            }
        }

    },function (err) {
        console.log(err)
    })

    //职位改变
    $scope.positionChang = function () {
        console.log('laile');
    }
    //清空按钮
    $scope.clean = function () {
        console.log("清理")
        $scope.employeeName = ''
            $scope.age = '',
            $scope.email = '',
            $scope.phone = '',
            $scope.qq = '',
            $scope.telephone = '',
            $scope.address = '',
            $scope.postCode = '',
            $scope.nationality,
            $scope.birthPlace ='',
            $scope.birthday = '',
            $scope.faith = '',
            $scope.politicalStatus = '',
            $scope.idcard = '',
            $scope.soSeNum = '',
            $scope.major = '',
            $scope.bank = '',
            $scope.accountNumber = '',
            $scope.hobby = '',
            $scope.resume = '',
            $scope.familyRelative = '',
            $scope.remark = '',
            $scope.third_org = '',
            $scope.second_org = '',
            $scope.first_org = '',
            $scope.professional = ''
    }

    //提交按钮
    $scope.submit = function () {
    console.log($scope.first_org )
        //判断是否为空
        if($scope.first_org == '' || $scope.first_org == undefined ){

            alert("温馨提示，一级机构不能为空！")
            return
        }else if($scope.second_org == '' || $scope.second_org == undefined){
            alert("温馨提示，二级机构不能为空！")
            return
        }else if($scope.third_org == '' || $scope.second_org == undefined){
            alert("温馨提示，三级机构不能为空！")
            return
        }else if($scope.position_type == '' || $scope.second_org == position_type){
            alert("温馨提示，职位分类不能为空！")
            return
        }else if($scope.position_name == '' || $scope.second_org == position_name){
            alert("温馨提示，职位名称不能为空！")
            return
        }else if($scope.position_name == '' || $scope.second_org == position_name){
            alert("温馨提示，职位称呼不能为空！")
            return
        }

        var creatTime =  document.getElementById('creatTime').value
        // console.log( creatTime)
        if($scope.first_org == "集团"){
            $scope.first = '01'

        }

// console.log($scope.second_org == "百度", $scope.third_org )
            if($scope.second_org == "百度"){
                $scope.second = '01'

            }else if($scope.second_org == "阿里巴巴"){
                console.log('laile ')
                $scope.second = '02'

            }else if($scope.second_org == "腾讯"){
                $scope.second = '03'
            }


        console.log($scope.second, $scope.first )

            if($scope.third_org == '开发组' && $scope.second_org == "百度" ){
                $scope.third = 11
            }else if($scope.third_org == '测试组' && $scope.second_org == "百度"){
                $scope.third = 12

            }else if($scope.third_org == '外包组' && $scope.second_org == "百度") {
                $scope.third = 13
            }else if($scope.third_org == '开发组' && $scope.second_org == "阿里巴巴"){
                $scope.third = 21
            }else if($scope.third_org == '测试组' && $scope.second_org == "阿里巴巴"){
                $scope.third = 22
            }else if($scope.third_org == '外包组' && $scope.second_org == "阿里巴巴"){
                $scope.third = 23
            }else if($scope.third_org == '开发组' && $scope.second_org == "腾讯"){
                $scope.third = 31
            }else if($scope.third_org == '测试组' && $scope.second_org == "腾讯"){
                $scope.third = 32
            }else if($scope.third_org == '外包组' && $scope.second_org == "腾讯"){
                $scope.third = 33
            }
        console.log($scope.first, $scope.second, $scope.third )

        if($scope.position_type == '程序员'){
                $scope.professional = '1'
        }

        //双向绑定数据
        console.log($scope.employeeName)
        console.log($scope.age)
        console.log($scope.sex)
        console.log($scope.email)
        console.log($scope.phone)
        console.log($scope.qq)
        console.log($scope.telephone)
        console.log($scope.address)
        console.log($scope.postCode)
        console.log($scope.nationality)
        console.log($scope.birthPlace)
        console.log($scope.birthday)
        console.log($scope.nation)
        console.log($scope.faith)
        console.log($scope.politicalStatus)
        console.log($scope.idcard)
        console.log($scope.soSeNum)
        console.log($scope.edubg)
        console.log($scope.major)
        console.log($scope.bank)
        console.log($scope.accountNumber)
        console.log($scope.hobby)
        console.log($scope.resume)
        console.log($scope.familyRelative)
        console.log($scope.remark)
        console.log($scope.third)
        console.log($scope.second)
        console.log($scope.first)
        console.log($scope.professional)
        console.log(creatTime)
        console.log($scope.registrant, $scope.Id)





        $scope.addEmployee(
            $scope.employeeName,
            $scope.age,
        $scope.sex,
        $scope.email,
        $scope.phone,
        $scope.qq,
        $scope.telephone,
        $scope.address,
        $scope.postCode,
        $scope.nationality,
        $scope.birthPlace,
        $scope.birthday,
        $scope.nation,
        $scope.faith,
        $scope.politicalStatus,
        $scope.idcard,
        $scope.soSeNum,
        $scope.edubg,
        $scope.major,
        $scope.bank,
        $scope.accountNumber,
        $scope.hobby,
        $scope.resume,
        $scope.familyRelative,
        $scope.remark,
        $scope.third,
        $scope.second,
        $scope.first,
        $scope.professional,
        creatTime,
            $scope.Id,// $scope.registrant,
         function (res) {
             window.location.hash = "#/resultEmployee"
             console.log(res)
         },
         function (err) {
             console.log(err)
         }
            )
return
        //档案登记
        $scope.addEmployee(
            '1',
            '1',
            '男',
            '1',
            '1',
            '1',
            '1',
            '1',
            '1',
            '中国',
            '1',
            'Tue Dec 12 2017 00:00:00 GMT+0800 (中国标准时间)',//'2017-12-09',
            '蒙古族',
            '1',
            '1',
            '1',
            '1',
            '大学',
            '1',
            '1',
            '1',
            '1',
            '1',
            '1',
            '1',
            '11',
            '01',
            '11',
            '1',
            '2017-12-10 15:07:21',
            '1',
            function (res) {
                console.log(res)
            },
            function (err) {
                console.log(err)
            }
        )

    }



  //档案查询
  //  $scope.selectEmployee(function (res) {
  //        console.log(res)
  //    },function (err) {
  //        console.log(err)
  //    })

    //档案修改






}])

//人事资源管理查询
    .controller('selectEmployeeCtrl', ['$scope', '$http', function ($scope, $http){
        $scope.professional = 1
        $scope.position_Type = []
        $scope.position_Name = []
        $scope.position_Title = []

        //获取所有机构名
        $scope.selectOrg(function (res) {
            console.log(res)
            $scope.firstOrgList = res.first_org
            $scope.sencondOrgList = res.second_org
            $scope.thirdOrgList = res.third_org
        },function (err) {
            console.log(err)
        })

        //获取所有职称名
        $scope.selectPosition(function (res) {
            console.log(res)
            $scope.position = res.data



            //循环数组
            for(var i = 0; i <= res.data.length-1; i++){

                // //获取职位分类
                for(var j = 0; j <=  $scope.position_Type.length; j++){
                    if( $scope.position_Type.indexOf(res.data[i].position_type) == -1){
                        $scope.position_Type.push(res.data[i].position_type)
                    }
                }
                //获取职位名称
                for(var j = 0; j <=  $scope.position_Name.length; j++){
                    if( $scope.position_Name.indexOf(res.data[i].position_name) == -1){
                        $scope.position_Name.push(res.data[i].position_name)
                    }
                }
                //职位称呼
                for(var j = 0; j <=  $scope.position_Title.length; j++){
                    if( $scope.position_Title.indexOf(res.data[i].position_title) == -1){
                        $scope.position_Title.push(res.data[i].position_title)
                    }
                }
            }

        },function (err) {
            console.log(err)
        })


        //清空按钮
        $scope.clean = function () {
                console.log("清理")
                $scope.third_org = '',
                $scope.second_org = '',
                $scope.first_org = '',
                $scope.position_type = ''
                $scope.position_name = ''
                $scope.position_title = ''
        }

        //开始时间
        $scope.timeChange = function () {

        }

        //查询按钮点击
        $scope.selectSubmit = function () {
            // console.log('tiaozhuan');
            window.location.hash = "#/resultEmployee"
        }
    }])
//人事资源管理结果
    .controller('resultEmployeeCtrl', ['$scope', '$http', function ($scope, $http){
         $scope.switchSelEmployee(function (res) {
             // console.log(res.data)
             for(var i= 0 ; i <= res.data.length -1; i++){
                 // console.log('12',res.data.isCheck)
                 //    console.log(res.data.length)
                 if( res.data[i].isCheck == 0){
                     res.data[i].isCheck = '未复核'
                 }else{
                     res.data[i].isCheck = '已复核'
                 }

             }

             for (var j = res.data.length -1; j>=0; j--) {
                 console.log("123")
                 if (res.data[j].isDelete == 1) {
                         // console.log('已经删除')
                     res.data.splice(j, 1);
                 }
             }


             // console.log(res.data)
             $scope.employees = res.data

         },function (err) {
             console.log(err)
         })

        $scope.black = function () {
            window.location.hash = "#/selectEmployee"

        }
        //选中档案
        $scope.selectFileNum = function (fileNum, name) {
            console.log(fileNum, name)

            var con;
            con=confirm(`你确定要查看${name}的文档吗`); //在页面上弹出对话框
            if(con==true){
                //查找档案
                selectNum = fileNum
                fileName = name
                console.log(fileName)
                window.location.hash = "#/updateEmployee"
                $scope.selectEmployee(fileNum, function (res) {
                    console.log(res)
                },function (err) {
                    cnosole.log(err)
                })
            }
            else{
                return
            }

        }
    }])
    //人事资源管理修改档案
    .controller('updateEmployeeCtrl', ['$scope', '$http', function ($scope, $http){
        // var str = location.hash.lastIndexOf('\?')
        //获取登记人
        $scope.registrant = document.getElementById('sysName').innerHTML
        //获取登记人的Id
        $scope.Id = document.getElementById('ID').innerHTML

        //获取登记人的职位
        $scope.pos = document.getElementById('sysPos').innerHTML

        console.log(selectNum, Id, pos, fileName)
        var fileNum = selectNum//201701011126
        var Name = fileName
        // var fileNum = location.hash.las

       $scope.selectEmployee(fileNum, function (res) {

            $scope.employeess = res.data
            console.log($scope.employeess[0] )
           $scope.name = $scope.employeess[0].name
           $scope.age = $scope.employeess[0].age
        },function (err) {
            cnosole.log(err)
        })

        //修改姓名
        $scope.nameChange = function () {
            // alert('修改')
            setTimeout(function () {
                var employeeName =document.getElementById('nameChange').innerHTML
                $scope.name = employeeName
                console.log( $scope.name)
            },1000)
        }


        //修改年龄
        $scope.ageChange = function () {
            // alert('修改')
            setTimeout(function () {
                var age =document.getElementById('ageChange').innerHTML
                $scope.age = age
                console.log( $scope.age)
            },1000)
        }

        //修改档案
        $scope.updateBtn = function () {

            var con;
            con=confirm(`你确定修改${fileNum}的文档吗`); //在页面上弹出对话框
            if(con==true){
                //查找档案
                console.log('修改档案',fileNum, $scope.name, $scope.age);
                if(fileNum == '' || undefined){
                    retrun
                }

                $scope.updateEmployee(fileNum, $scope.name, $scope.age, function (res) {
                    console.log(res)
                    alert('修改档案成功')
                    window.location.hash = "#/resultSalary"
                },function (err) {
                    cnosole.log(err)
                })
            }
            else{
                return
            }
        }
        //复核档案
        $scope.checkBtn = function () {
            console.log('复核档案');
            if($scope.pos !== '人事经理'){
                alert('抱歉，你没有复核的权限！')
                return
            }
            var con;
            con=confirm(`你确定复核${fileNum}的文档吗`); //在页面上弹出对话框
            if(con==true){
                //查找档案


                $scope.checkEmployee(fileNum, function (res) {
                    console.log(res)
                    alert('复核档案成功')
                    window.location.hash = "#/resultEmployee"
                },function (err) {
                    cnosole.log(err)
                })
            }
            else{
                return
            }
        }

        //删除档案
        $scope.deleteBtn  = function () {
        console.log('删除档案')
            if($scope.pos !== '人事经理'){
                alert('抱歉，你没有删除的权限！')
                return
            }
            var con;
            con=confirm(`你确定要删除${fileNum}的文档吗`); //在页面上弹出对话框
            if(con==true){
                //查找档案


                $scope.deleteEmployee(fileNum, function (res) {
                    console.log(res)
                    alert('删除档案成功')
                    window.location.hash = "#/resultEmployee"
                },function (err) {
                    cnosole.log(err)
                })
            }
            else{
                return
            }
        }

        //返回档案查询
        $scope.backBtn  = function () {
                    window.location.hash = "#/resultEmployee"
        }





    }])